package com.example.userservice.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;
import java.util.Random;

/**
 * Created with IntelliJ IDEA 2023.
 * Description:
 * User: 故我
 * Date: 2024-02-13
 * Time: 15:32
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ServletWebServerApplicationContext context;

    @RequestMapping("/getname")
    public String getName() {
        return "Port: " + context.getWebServer().getPort() +
                " |  Service: " + new Random().nextInt(100);
    }

    @RequestMapping("/print-header")
    public void PrintHeader(HttpServletRequest request) {
        Enumeration<String> headers = request.getHeaderNames();
        while (headers.hasMoreElements()) {
            String key = headers.nextElement();
            // 根据 key 从request 中获取 value
            String value = request.getHeader(key);
            System.out.println("key: " + key + " | value: " + value);
        }
    }

    @RequestMapping("/504")
    public void ReturnError(HttpServletResponse response) {
        System.out.println("---------------- Return 504 -------------------");
        response.setStatus(504);
    }
}
